package com.qfedu.bookstores.Filter;

import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Objects;

//后台操作权限验证
@WebFilter(value={"/pages/manager/*","/manager/bookServlet"})
public class ManagerFilter implements Filter {
    public void destroy() {
    }

    public void doFilter(ServletRequest req, ServletResponse resp, FilterChain chain) throws ServletException, IOException {
        //过滤中确认登录信息即可,数据是存session对象作用域中
        //只要确认作用域中是否存在登录信息就可以判断是否登录
        HttpServletRequest request = (HttpServletRequest)req;
        HttpServletResponse response = (HttpServletResponse)resp;
        Object user = request.getSession().getAttribute("user");
        if(Objects.isNull(user)){
            request.getRequestDispatcher("/pages/user/login.jsp").forward(request, response);
        }else {
            chain.doFilter(request , response);
        }
    }

    public void init(FilterConfig config) throws ServletException {

    }

}
